package com.book.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.api.RankingApi;
import com.book.mapper.ShoucangDao;
import com.book.pojo.CollectRanking;
import com.book.pojo.Shoucang;
import com.book.service.ShoucangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxy
 * @since 2023-10-13 15:50:00
 */
@Service
public class ShoucangServiceImpl extends ServiceImpl<ShoucangDao, Shoucang> implements ShoucangService {

    @Autowired
    private ShoucangDao shoucangDao;

    @Autowired
    private RankingApi collectRankingApi;

    @Override
    public int insertShoucang(Long rid, Long bid) {
        //查询该作者已收藏的bookId
        //1:已收藏  2:收藏成功
        List<Long> bids = shoucangDao.selectBidsByRid(rid);
        for (Long aLong : bids) {
            if (aLong==bid){
                return 1;
            }
        }
        shoucangDao.insertShoucang(rid,bid);
        Shoucang s=shoucangDao.selectBookNumByBid(bid);
        collectRankingApi.deleteBybid(bid);
        CollectRanking collectRanking = new CollectRanking();
        collectRanking.setNumber(s.getNumber());
        collectRanking.setBookId(s.getBookId());
        collectRankingApi.addCollectRanking(collectRanking);
        return 2;
    }

    @Override
    public int deleteShoucang(Long rid, Long bid) {
        //查询该作者已收藏的bookId
        List<Long> bids = shoucangDao.selectBidsByRid(rid);
        //判断是否收藏过该作品  1:未收藏  2:取消收藏成功
        for (Long aLong : bids) {
            if (aLong!=bid){
                return 1;
            }
        }
        shoucangDao.deleteShoucang(rid,bid);
        Shoucang s=shoucangDao.selectBookNumByBid(bid);
        collectRankingApi.deleteBybid(bid);
        CollectRanking collectRanking = new CollectRanking();
        collectRanking.setNumber(s.getNumber());
        collectRanking.setBookId(s.getBookId());
        collectRankingApi.addCollectRanking(collectRanking);
        return 2;
    }

    @Override
    public List<Long> selectBidsByRid(Long rid) {
        return shoucangDao.selectBidsByRid(rid);
    }

    @Override
    public void selectBookNumByBid(Long bid) {
         shoucangDao.selectBookNumByBid(bid);
    }

    @Override
    public  List<Shoucang> CollectRanking() {
        return shoucangDao.CollectRanking();
    }
}
